package com.example.gpstracking.netandtask;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.gpstracking.object.Data;
import com.example.gpstracking.object.DataMail;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class NetSupport {
	
	public static boolean isNetWorkAvaible(Context context)
	{
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		
		NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
		if(activeNetwork != null && activeNetwork.isConnectedOrConnecting())
			return true;
		return false;
	}
	/**
	 * turn on 3g for 2.3 and above
	 * @param context
	 */
	public static void turnOnDataTrans(Context context){
		
		final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	    Class conmanClass;
		try {
			
				conmanClass = Class.forName(conman.getClass().getName());
				final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
			    iConnectivityManagerField.setAccessible(true);
			    final Object iConnectivityManager = iConnectivityManagerField.get(conman);
			    final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
			    final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
			    setMobileDataEnabledMethod.setAccessible(true);
			    //set
			    setMobileDataEnabledMethod.invoke(iConnectivityManager, true);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   
	}
	/**
	 * turn off 3g for 2.3 and above
	 * @param context
	 */
	public static void turnOFFDataTrans(Context context){
		
		final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	    Class conmanClass;
		try {
			
				conmanClass = Class.forName(conman.getClass().getName());
				final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
			    iConnectivityManagerField.setAccessible(true);
			    final Object iConnectivityManager = iConnectivityManagerField.get(conman);
			    final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
			    final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
			    setMobileDataEnabledMethod.setAccessible(true);
			    //set
			    setMobileDataEnabledMethod.invoke(iConnectivityManager, false);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   
	}
	public static String JsonConvert(ArrayList<Data> arrayList) throws JSONException
	{
		//ArrayList<DataMail> dataMails = new ArrayList<DataMail>();
		//HashMap<Integer, DataMail>
		ArrayList<JSONObject> jsonObjects = new ArrayList<JSONObject>();
		Calendar calendar = Calendar.getInstance();
		//
		for(Data item : arrayList)
		{
			calendar.setTimeInMillis(item.getTime());
			String time = calendar.getTime().toString();
			JSONObject itemJson  =  buildJsonObj(new DataMail(time, item.getLattitude(), item.getLongtiude()));
			if(itemJson != null)
			{
				jsonObjects.add(itemJson);
			}
			continue;
			
			//dataMails.add(new DataMail(time, item.getLattitude(), item.getLongtiude()));
		}
		//
		JSONArray jsonArray = new JSONArray(jsonObjects);
		Log.d("JSON", jsonArray.toString());
		return jsonArray.toString();		
	}
	public static JSONObject buildJsonObj(DataMail dataMail) throws JSONException
	{
		if(dataMail != null)
		{
			JSONObject jsonObject = new JSONObject();			
			jsonObject.put("long", dataMail.getLongtiude());
			jsonObject.put("lat", dataMail.getLattitude());
			jsonObject.put("time", dataMail.getTime());
			return jsonObject;
		}
		return null;
		
		
	}

}
